<?php
class Themes {
	public $baseDir = 'templates/themes/';
	function getAvailableThemes() {
		$themes = scandir($this->baseDir);
//		var_dump($this->baseDir);
//		var_dump($themes);
		foreach($themes as $key => $entry){
			if ($entry==='..' || $entry==='.' || !is_dir($this->baseDir.$entry) || !file_exists($this->baseDir.$entry.'/index.html')) {
				unset($themes[$key]);
			}
		}
		return $themes;
	}
	function getCurrentTheme() {
		if (isset($_COOKIE['theme'])) {
			$theme = $_COOKIE['theme'];
			//var_dump($theme);
			//var_dump($this->getAvailableThemes());
			if (in_array($theme,$this->getAvailableThemes())) {
				return $theme;
			}
		}
		return $this->getDefaultTheme();
	}
	function setTheme($theme) {
		setcookie('theme', $theme, time()+60*60*24*30, '/');
		$_COOKIE['theme'] = $theme;
	}
	function getDefaultTheme() {
		$themes = $this->getAvailableThemes();
		if (count($themes) < 1) {
			trigger_error("No theme available", E_USER_ERROR);
			die();
		}
		reset($themes);
		return current($themes);
	}
	function getThemeDir() {
		return $this->baseDir.$this->getCurrentTheme().'/';
	}
	function getSelectorData() {
		$themes = $this->getAvailableThemes();
		$result = array();
		$current = $this->getCurrentTheme();
		foreach($themes as $entry){
			$result[] = array('name'=>$entry,'active' => ($entry === $current));
		}
		return $result;
	}

}
